﻿@inherits BaseDialogPage<DepartmentTreeModel>

<ValidateForm class="form-inline" Model="@Model" OnInvalidSubmit="@OnInvalidSubmit">
    <div class="row">
        <div class="form-group col-12">
            <BootstrapInput @bind-Value="@Model.ParentDeptName" IsDisabled="true" />
        </div>
        <div class="form-group col-12">
            <BootstrapInput @bind-Value="@Model.Name"/>
        </div>
        <div class="form-group col-12">
            <BootstrapInput @bind-Value="@Model.DisplayOrder"/>
        </div>
        <div class="form-group col-12">
            <Checkbox @bind-Value="@Model.IsEnabled" />
        </div>
        <div class="form-group col-12">
            <BootstrapInput @bind-Value="@Model.Remarks" />
        </div>
        <div class="form-group col-12">
            <Button ButtonType="ButtonType.Submit" OnClickWithoutRender="SaceAsync" IsAsync="true" Text="保存" />
        </div>
    </div>
</ValidateForm>
<SweetAlert />

@code {


    [CascadingParameter(Name = "BodyContext")]

    private object ObjectModel { set; get; }

    private DepartmentModel ParentDept
    {
        get
        {
            return (ObjectModel as DepartmentModel);

        }
    }

    protected async override Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            if (ObjectModel != null)
            {
                Model.ParentDeptId = ParentDept.Id;
                Model.ParentDeptName = ParentDept.Name;
                Model.IsEnabled = true;//默认开启
                await InvokeAsync(StateHasChanged);
            }
        }
        await base.OnAfterRenderAsync(firstRender);
    }

    private async Task SaceAsync()
    {
        var operateResult = await AccountClientFactory.Dept.CreateDepartment(Model);
        if (operateResult.IsSuccess == true)
        {
            await this.Alert("保存成功!");
        }
        else
        {
            await this.Alert("保存失败!" + operateResult.Message);
        }
    }

    private async Task OnInvalidSubmit(EditContext context)
    {

        await Task.CompletedTask;
    }

}
